<?PHP $this->Html->script('jquery-1.7.2.min', false); ?>
<?PHP $this->Html->script('jquery-ui-1.8.18.custom.min', false); ?>
<?PHP $this->Html->script('gestionDevis', false); ?>
<?PHP $this->Html->css('devis_prefinal', null, array('inline' => false)); ?>
<?PHP $this->Html->css('jquery-ui-1.8.18.custom.css', null, array('inline' => false)); ?>

<form action="../enregistrerDevis" method="POST">
	<div id="outil_devis">
		<div id="titre_outil_devis">
			Actions
		</div>
		<?PHP if($infos['devis']['statut_devis'] != 'validé') { ?>
			<div>
				<input class="vox_bouton" type="submit" value="Enregistrer les modifications" name="enregistrement" /><br />
				<?PHP if($this->Session->read('utilisateur.estAdmin')) { ?>
					<input class="vox_bouton" type="submit" value="Accepter et valider le devis" name="validation" onclick="return acceptation_validation_devis();" /><br />
				<?PHP }?>
				<input class="vox_bouton" type="button" value="Réinitialiser" onclick="initialiserDevis();" />
			</div>
		
			<div class="question_devis">
				Ajouter un article
			</div>
			<div id="boite_ajout_article">
				<div>
					Catégorie :
					<select id="select_categorie">
					</select>
				</div>
				<div>
					Article :
					<select id="select_article">
					</select>
				</div>
				<input type="button" value="Ajouter cet article" onclick="boutonAjouterArticle($('#select_categorie').val(), $('#select_article').val(), categories)" />
			</div>
			
			<?PHP foreach($questions_finalisation as $q): ?>
				<?PHP switch($q['question']['type_reponse']) {
					case "LISTE" :
				?>
					<div class="question_devis">
						<?PHP echo $q['question']['libelle_question']; ?>
					</div>
					<div>
						<input type="hidden" value="<?PHP echo $q['question']['libelle_question']; ?>" name="question_devis[<?PHP echo $q['question']['id_question']; ?>][libelle]" />
						<input type="hidden" value="<?PHP echo $q['question']['type_reponse']; ?>" name="question_devis[<?PHP echo $q['question']['id_question']; ?>][type]" />
						<select onchange="appliquerReglesDevis(regle_devis)" name="question_devis[<?PHP echo $q['question']['id_question']; ?>][val]">
							<option></option>
						<?PHP foreach($q['reponses'] as $r):
							foreach($reponses_finalisation as $donnee):
								// on parcours la(les) reponse(s) donnee(s) à cette question pour la(les) afficher par defaut
								if($donnee['rep']['id_reponse'] == $r['id_reponse'] && isset($donnee['rep']['id_reponse']) && !empty($donnee['rep']['id_reponse'])) {  ?>
									<option id="reponse_devis_<?PHP echo $r['id_reponse'];?>" value="<?PHP echo $r['id_reponse'];?>" selected="selected"><?PHP echo $r['libelle_reponse_combo']; ?></option>
								<?PHP }
								else { ?>
									<option id="reponse_devis_<?PHP echo $r['id_reponse'];?>" value="<?PHP echo $r['id_reponse'];?>"><?PHP echo $r['libelle_reponse_combo']; ?></option>
								<?PHP }
							endforeach;
						endforeach; ?>
						</select>
					</div>
				<?PHP break;
				} ?>
			<?PHP endforeach; ?>
			<!--<div class="question_devis">
				Remise
			</div>
			<div>
				<input type="number" value="<?PHP echo $infos['devis']['remise_devis'];?>" min="0" max="100" name="remise" />
			</div>-->
		<?PHP }
		else { ?>
			<div>
				<input class="vox_bouton" type="submit" value="format PDF" name="formatPDF" />
				<input class="vox_bouton" type="submit" value="Devis Accepté" name="accept" onclick="return accepterDevis();"/>
				<input class="vox_bouton" type="submit" value="Devis refusé" name="refuse" onclick="return refuserDevis();"/>
			</div>
			
			<?PHP foreach($questions_finalisation as $q): ?>
				<?PHP switch($q['question']['type_reponse']) {
					case "LISTE" :
				?>
					<div class="question_devis">
						<?PHP echo $q['question']['libelle_question']; ?>
					</div>
					<div>
						<input type="hidden" value="<?PHP echo $q['question']['libelle_question']; ?>" name="question_devis[<?PHP echo $q['question']['id_question']; ?>][libelle]" />
						<input type="hidden" value="<?PHP echo $q['question']['type_reponse']; ?>" name="question_devis[<?PHP echo $q['question']['id_question']; ?>][type]" />
						<?PHP foreach($q['reponses'] as $r):
							foreach($reponses_finalisation as $donnee):
								// on parcours la(les) reponse(s) donnee(s) à cette question pour la(les) afficher par defaut
								if($donnee['rep']['id_reponse'] == $r['id_reponse'] && isset($donnee['rep']['id_reponse']) && !empty($donnee['rep']['id_reponse'])) {  ?>
									<input type="hidden" name="<?PHP echo 'question_devis['.$q['question']['id_question'].'][val]'; ?>" value="<?PHP echo $r['libelle_reponse_combo'];?>" />
									<div><?PHP echo $r['libelle_reponse_combo']; ?></div>
								<?PHP }
							endforeach;
						endforeach; ?>
					</div>
				<?PHP break;
				} ?>
			<?PHP endforeach; ?>
			<!--<div class="question_devis">
				Remise
			</div>
			<div>
				<input type="hidden" name="remise" value="<?PHP echo $infos['devis']['remise_devis']; ?>" />
				<?PHP echo (isset($infos['devis']['remise_devis']) && !empty($infos['devis']['remise_devis']) && $infos['devis']['remise_devis'] > 0) ? $infos['devis']['remise_devis'].' %' : "Aucune";?>
			</div>-->
		<?PHP }?>
	</div>

	<div id="devis">
		<table id="table_header">
			<tr>
				<td rowspan="2">
					<?PHP echo $this->Html->image('logo_devis.png', array('alt' => 'logo devis', 'id' => 'logo_devis')); ?>
				</td>
				<td class="devis_info">
					<table class="devis_contact">
						<tr>
							<th>Date</th>
							<td><?PHP echo strftime("%d %b %Y", strtotime($infos['devis']['date_creation'])); ?></td>
						</tr>
						<tr>
							<th style="visibility:hidden;">&nbsp;</th>
							<td> </td>
						</tr>
						<tr>
							<th>Votre contact commercial</th>
							<td><?PHP echo $infos['utilisateur']['nom_utilisateur']; ?></td>
						</tr>
						<tr>
							<th>Mail</th>
							<td><?PHP echo $infos['utilisateur']['mail']; ?></td>
						</tr>
						<tr>
							<th>T&eacute;l&eacute;phone</th>
							<td><?PHP echo $infos['utilisateur']['telephone']; ?></td>
						</tr>
						<tr>
							<th>Fax</th>
							<td><?PHP echo $infos['utilisateur']['fax']; ?></td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td class="devis_info">
					<table class="devis_contact">
						<tr>
							<th>Soci&eacute;t&eacute;</th>
							<td><?PHP echo $infos['Societe']['raison_sociale']; ?></td>
						<tr>
						<tr>
							<th>Contact</th>
							<td><?PHP echo $infos['Societe']['contact_administratif']; ?></td>
						<tr>
						<tr>
							<th>Adresse</th>
							<td><?PHP echo $infos['Societe']['adresse_facturation']; ?></td>
						<tr>
						<tr>
							<th>Code Postal</th>
							<td><?PHP echo $infos['Societe']['code_postal_societe']; ?></td>
						<tr>
						<tr>
							<th>Ville</th>
							<td><?PHP echo $infos['Societe']['ville_societe']; ?></td>
						<tr>
						<tr>
							<th>Telephone</th>
							<td><?PHP echo $infos['Societe']['telephone_administratif']; ?></td>
						<tr>
						<tr>
							<th>Adresse mail</th>
							<td><?PHP echo $infos['Societe']['mail_administratif']; ?></td>
						<tr>
					</table>
				</td>
			</tr>
		</table>
		<div style="text-align:right;font-size:18px;font-weight:bold;">Proposition commerciale n&deg;: <?PHP echo $infos['devis']['ref_devis']; ?></div>
		
		<table id="contenu_devis" class="tableau_devis">
			
		</table>
	</div>

	<?PHP
		$script = "var modifiable;";
		// pour chaque article dans les reponses données dans le questionnaire, on ajoute le groupe et l'article dans le script js pour générer la page de finalisation
		// articles_reponses est contruit dans le modele de Devi
		foreach($articles_reponses as $rep) {
			if($infos['devis']['statut_devis'] == 'validé') {
				$script .= "modifiable = false;";
			}
			else {
				$script .= "modifiable = true;";
			}
			$l = $rep['ligne_devis'];
			$script .= "ajouterBloc(".$l['id_bloc_devis'].", '".$l['libelle_bloc_devis']."', ".$l['rang_bloc_devis'].");";
			$script .= "ajouterCategorie(".$l['id_groupe_devis'].", '".$l['libelle_groupe_devis']."', ".$l['id_bloc_devis'].", '".$l['libelle_bloc_devis']."', ".$l['rang_groupe_devis'].", ".$l['rang_bloc_devis'].");";
			$script .= "ajouterArticle({id:".$l['id_bloc_devis'].", libelle:'".$l['libelle_bloc_devis']."'}, ".
										"{id:".$l['id_groupe_devis'].", libelle:'".$l['libelle_groupe_devis']."'}, ".
										"{code : '".$l['ref_article']."', ".
										"description : '".$l['description_article']."', ".
										"quantite : ".$l['quantite_article'].", ".
										"prix : ".$l['tarif_brut'].", ".
										"id : ".$l['id_article'].", ".
										"supprimable : modifiable, ".
										"quantite_modifiable : modifiable ".
										"}, true);";
		}
		
	?>

	<script type="text/javascript">
		var categories = <?PHP echo php2js($categories); ?>;
		var regles_ligne_ligne = <?PHP echo php2js($regle_ligne_ligne); ?>;
		var regle_devis = <?PHP echo php2js($regle_devis); ?>;
		
		function initialiserDevis() {
			viderDevis();
			appliquerListeRegleLigneLigne(regles_ligne_ligne);
			mettreAJourLesCategorieDArticle("select_categorie", "select_article", categories);
			<?PHP echo $script; ?>
		}
		
		$(function() {
			initialiserDevis();
		});
	</script>
</form>